<template>
  <div class="cartDetail">
    <div class="content">
      <van-checkbox-group v-model="checked" @change="groupChange">
        <div v-for="(item, index) in store.state.cartSum" :key="index">
          <ListItem :itemList="item" :showCheck="true" :changeNum="changeNum" />
        </div>
      </van-checkbox-group>
    </div>
    <van-submit-bar :price="allPrice"
    button-color="#ffc400" 
    button-text="提交订单"
     @submit="onSubmit" 
     class="submit-all" v-if="store.state.isDelete">
      <van-checkbox v-model="submitChecked"
       @click="choseAll"
      checked-color="#ffc400">全选</van-checkbox>
    </van-submit-bar>
    <div class="buy" v-else>
      <div class="left">
        <van-checkbox v-model="submitChecked"
        @click="choseAll"
        checked-color="#ffc400">全选</van-checkbox>
      </div>
      <div class="delete" @click="handleRomove">删除</div>
    </div>
  </div>
</template>

<script>
import ListItem from "@/components/ListItem.vue";
import { reactive, toRefs, onMounted, computed } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { showToast } from 'vant';
import 'vant/es/toast/style';
export default {
  components: { ListItem },
  props:['changeShow'],
  setup(props) {
    const store = useStore();
    const router = useRouter()
    const data = reactive({
      checked: [],
      submitChecked:true,
      price:0
    });
    const changeNum = (value, detail) => {
      store.state.cartSum.map((item) => {
        if (item.id === detail.name) {
          item.num = value;
        }
      });
    };
    const init = () => {
      data.checked = store.state.cartSum.map((item) => item.id);
      // console.log(store.state.cartSum.map((item) => item.id));
    };
    const allPrice = computed(()=>{
      
      let countList = updateData()
      
      let num = 0
      countList.forEach(item => {
        num += item.num * item.price
      }); 
      return num*100
    })
    const onSubmit = ()=>{
      if(data.checked.length){
        store.commit('pay',updateData())
        // console.log(store.state.orderList);
        router.push({
          path:'/ordercreate',
          query:{
            list:data.checked
          }
        })
      }else{
        showToast('请选择订单！！')
      }
    }
    const choseAll = ()=>{
      // 第一种情况：有商品有选中，但没有完全选中，此时就要商品全部选中
      if(data.checked.length !== store.state.cartSum.length){
        init()
      }else{
        // 第二种情况：反选
        data.checked = []
      }
    }
    const groupChange = (result)=>{
      // console.log(result);
      if(result.length !== store.state.cartSum.length){
        data.submitChecked = false
      }else{
        data.submitChecked = true
      }
      data.checked = result
    }
    const updateData = (type)=>{
      return store.state.cartSum.filter(item=>{
        return type === 'delete'? ! data.checked.includes(item.id) : data.checked.includes(item.id)
      })
    }
    const handleRomove = ()=>{
      // 部分删除
      // 全部删除
      if(data.checked.length){
        store.commit('deleteList',updateData('delete'))
        data.checked = []
        // 购物车删除完时
        if(!store.state.cartSum.length){
          props.changeShow()
          store.commit('changeDelete')
        }
      }else{
        alert('请选择删除的商品')
      }
      

    }
    onMounted(() => {
      init();
      
    });

    return {
      ...toRefs(data),
      store,
      changeNum,
      onSubmit,
      choseAll,
      groupChange,
      allPrice,
      handleRomove
    };
  },
};
</script>

<style lang="less" scoped>
.cartDetail {
  font-size: 14px;
  flex: 1;
  position: relative;
  overflow-y: auto;
  padding: 20px 20px 55px;
  .submit-all {
    position: fixed;
    bottom: 63px;
  }

  .buy {
    position: fixed;
    bottom: 63px;
    right: 0;
    display: flex;
    justify-content: space-between;
    width: 100%;
    background-color: #fff;
    border-radius: 10px;
    height: 50px;
    align-items: center;
    padding: 0 16px;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
    }
    .delete {
      color: #fff;
      background-color: #ffc400;
      border-radius: 20px;
      font-size: 14px;
      font-weight: 600;
      width: 110px;
      height: 40px;
      text-align: center;
      line-height: 40px;
    }
  }

  .content {
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
  }
}
</style>
